<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img class="el-header-img" src="../assets/icon/管理中心.png" width="12%" alt="" @click="doWelcome">
        <span>Vue Element Admin</span>
      </div>
      <el-button type="info" @click="logout">登出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏折叠 -->
        <div class="toggle-menu" @click="toggleCollapseMenu">
          |||
        </div>
        <!-- 侧边栏菜单 -->
        <!-- router: elementUI 开启路由模式 -->
        <!-- default-active 设置高亮 -->
        <el-menu background-color="#1e2935" text-color="#fff" active-text-color="#409BFF" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <!-- 循环一级菜单思路: v-for绑定, 用item.id作为唯一key标识, 打开菜单的index利用拼接空串转化数字成字符串 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="savaNavState('/'+subItem.path)">
              <!-- 图标 -->
              <i class="el-icon-s-unfold"></i>
              <!-- 文本 -->
              <span>{{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      // 菜单图标
      iconsObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-view',
        101: 'el-icon-shopping-bag-1',
        102: 'el-icon-document-copy',
        145: 'el-icon-data-analysis'
      },
      // 折叠flag, 默认不折叠
      isCollapse: false,
      // 被激活的链接地址, 默认为空
      activePath: ''
    }
  },
  // 生命周期函数
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 登出
    logout () {
      // 清除 token
      window.sessionStorage.removeItem('token');
      this.$router.push('/login');
    },
    // 获取左侧权限菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      this.menuList = res.data
    },
    // 展开折叠菜单
    toggleCollapseMenu () {
      this.isCollapse = !this.isCollapse
    },
    // 保存导航栏子菜单的激活状态
    savaNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 点击 logo 回到欢迎页
    doWelcome () {
      // 清除导航栏状态信息
      window.sessionStorage.removeItem('activePath');
      // 路由到 home 页面实现刷新
      this.$router.push('/home')
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #344a5f;
  display: flex;
  justify-content: space-between;
  padding-left: 5px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #1e2935;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #dae3ec;
}
.home-container {
  height: 100%;
}
.toggle-menu {
  background-color: #373b4b;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
